package ink.onei.shopping.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import ink.onei.common.annotation.Log;
import ink.onei.common.core.controller.BaseController;
import ink.onei.common.core.domain.AjaxResult;
import ink.onei.common.enums.BusinessType;
import ink.onei.shopping.domain.Detail;
import ink.onei.shopping.service.IDetailService;
import ink.onei.common.utils.poi.ExcelUtil;
import ink.onei.common.core.page.TableDataInfo;

/**
 * 商品规格Controller
 *
 * @author nekotako
 * @date 2024-02-07
 */
@RestController
@RequestMapping("/shopping/detail")
public class DetailController extends BaseController {
    @Autowired
    private IDetailService detailService;

    /**
     * 查询商品规格列表
     */
    @PreAuthorize("@ss.hasPermi('shopping:goods:all')")
    @GetMapping("/list")
    public TableDataInfo list(Detail detail) {
        startPage();
        List<Detail> list = detailService.selectDetailList(detail);
        return getDataTable(list);
    }

    /**
     * 导出商品规格列表
     */
    @PreAuthorize("@ss.hasPermi('shopping:goods:all')")
    @Log(title = "商品规格", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Detail detail) {
        List<Detail> list = detailService.selectDetailList(detail);
        ExcelUtil<Detail> util = new ExcelUtil<>(Detail.class);
        util.exportExcel(response, list, "商品规格数据");
    }

    /**
     * 获取商品规格详细信息
     */
    @PreAuthorize("@ss.hasPermi('shopping:goods:all')")
    @GetMapping(value = "/{detailId}")
    public AjaxResult getInfo(@PathVariable("detailId") Long detailId) {
        return success(detailService.selectDetailByDetailId(detailId));
    }

    /**
     * 新增商品规格
     */
    @PreAuthorize("@ss.hasPermi('shopping:goods:all')")
    @Log(title = "商品规格", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Detail detail) {
        return toAjax(detailService.insertDetail(detail));
    }

    /**
     * 修改商品规格
     */
    @PreAuthorize("@ss.hasPermi('shopping:goods:all')")
    @Log(title = "商品规格", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Detail detail) {
        return toAjax(detailService.updateDetail(detail));
    }

    /**
     * 删除商品规格
     */
    @PreAuthorize("@ss.hasPermi('shopping:goods:all')")
    @Log(title = "商品规格", businessType = BusinessType.DELETE)
    @DeleteMapping("/{detailIds}")
    public AjaxResult remove(@PathVariable Long[] detailIds) {
        return toAjax(detailService.deleteDetailByIds(detailIds));
    }
}
